package com.woniu.liuzhougov.itemAndTask.controller;


import com.woniu.liuzhougov.authorityControl.pojo.User;
import com.woniu.liuzhougov.authorityControl.service.UserService;
import com.woniu.liuzhougov.authorityControl.service.impl.UserServiceImpl;
import com.woniu.liuzhougov.itemAndTask.pojo.ItemTask;
import com.woniu.liuzhougov.itemAndTask.service.ItemTaskService;
import com.woniu.liuzhougov.itemAndTask.vo.LayUIResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wyz02
 * @version 1.0
 * @ClassName ItemTaskController
 * @Description: 事务子任务控制器
 * @date 2019/4/22 10:40
 */
@Controller
@RequestMapping("itemTask")
public class ItemTaskController {

    @Autowired
    private ItemTaskService itemTaskService;

    @Autowired
    private UserService userItemService;

    /*
    **
     * @Author Wuyuzhong
     * @Description 根据事项id查询所有子任务列表
     * @Date 2019/4/22 10:56
     * @Param [itemId]
     * @return com.woniu.liuzhougov.businessproject.vo.Result
     **/
    @RequestMapping("/listById")
    @ResponseBody
    public LayUIResult searchTaskByItemId(Integer itemId){
        //System.out.println(itemId);

        List<ItemTask> itemTasks = itemTaskService.searchTaskByItemId(itemId);
        LayUIResult result = new LayUIResult(itemTasks);

        //System.out.println(result.getData());

        return result;
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 根据子任务id删除子任务
     * @Date 2019/4/22 11:24
     * @Param [taskId]
     * @return java.lang.String
     **/
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Integer taskId,Integer urengcyId){
        String result=itemTaskService.deleteByPrimaryKey(taskId,urengcyId);
        return result;
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 更新子任务信息
     * @Date 2019/4/22 11:44
     * @Param [itemTask]
     * @return java.lang.String
     **/
    @RequestMapping("/update")
    @ResponseBody
    public String updateTask(String taskInfo,Integer itemTaskWorker,Integer itemId,String itemUrengcy,Integer itemTaskId){
        String result = itemTaskService.updateTask(taskInfo,itemTaskWorker,itemId,itemUrengcy,itemTaskId);
        return result;
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 完成子任务，子任务状态(未完成：1；已完成：2)
     * @Date 2019/4/22 11:57
     * @Param [itemTaskId]
     * @return java.lang.String
     **/
    @RequestMapping("/finish")
    @ResponseBody
    public String updateTaskState(Integer itemTaskId,Integer urengcyId){
        String result = itemTaskService.updateTaskState(itemTaskId,2,urengcyId);
        return result;
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 通过子任务id查找任务详情
     * @Date 2019/4/22 12:01
     * @Param [itemTaskId]
     * @return com.woniu.liuzhougov.itemAndTask.pojo.ItemTask
     **/
    @RequestMapping("/detile")
    @ResponseBody
    public ItemTask detile(Integer itemTaskId){
        ItemTask itemTask = itemTaskService.searchTaskById(itemTaskId);
        //System.out.println(itemTask);

        return itemTask;
    }
    
    /*
    **
     * @Author Wuyuzhong
     * @Description 新建子任务
     * @Date 2019/4/22 12:03
     * @Param [itemTask]
     * @return java.lang.String
     **/
    @RequestMapping("/add")
    @ResponseBody
    public String addTask(String taskInfo,Integer itemTaskWorker, Integer itemId,String itemUrengcy){
        //System.out.println(11111111);
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        String result= itemTaskService.addTask(taskInfo,itemTaskWorker, itemId,itemUrengcy,user);
        return result;
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 用于新建子任务，根据部门id查找所有用户
     * @Date 2019/4/25 15:17
     * @Param []
     * @return java.util.List<com.woniu.liuzhougov.authorityControl.pojo.User>
     **/
    @RequestMapping("/users")
    @ResponseBody
    public List<User> searchUserByDepartment(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        List<User> users= userItemService.listUserByDepartmentId(user.getDepartment().getDepartmentId());

        //System.out.println(users);

        return users;
    }

    /*
    **
     * @Author Wuyuzhong
     * @Description 获取我的任务列表
     * @Date 2019/4/28 14:29
     * @Param []
     * @return java.util.List<com.woniu.liuzhougov.itemAndTask.pojo.ItemTask>
     **/
    @RequestMapping("myTaskList")
    @ResponseBody
    public LayUIResult searchMyTask(){
        User user = (User) SecurityUtils.getSubject().getPrincipal();   //获取登录信息
        List<ItemTask> itemTasks = itemTaskService.seachTaskByWorkerId(user.getUserId());
//        for (ItemTask itemTask:itemTasks) {
//            System.out.println(itemTask);
//        }

        LayUIResult result = new LayUIResult(itemTasks);
        return result;
    }

}
